package com.tzt.controller.apk;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.tzt.common.BaseController;
import com.tzt.contents.ErrorCodeConstant;
import com.tzt.exception.InnerException;
import com.tzt.service.APKService;

@RestController
public class APKController extends BaseController
{
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private APKService apkService;

    /**
     * 添加app安装包
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param version 版本
     * @param url 地址
     * @param fileName 文件名
     * @param assetsType 资产类型（0：柜子，1：门店）
     * @param doorType 门类型（1：单门，2：双门）
     * @param description apk的描述
     * @return
     */
    @RequestMapping(value = "/api/addAPK", method = RequestMethod.POST)
    public Map<String, Object> addAPK(String shopUserId, String token, String version, String url, String fileName,
            String assetsType, String doorType, String description)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            apkService.addAPK(shopUserId, token, version, url, fileName, assetsType, doorType, description);
        }
        catch (NumberFormatException e)
        {
            logger.error("新增APK失败：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("新增APK失败：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        return successData(map);
    }

    /**
     * 查询APK
     * @param page 页数
     * @param size 大小
     * @param keyword 关键字（主键id ；版本号）
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param loginRoleId 登录角色id
     * @param menuId 菜单id
     * @param assetsType 资产类型（0：柜子，1：门店）
     * @param doorType 门类型（1：单门，2：双门）
     * @param map
     * @return
     */
    @RequestMapping(value = "/api/getAPK", method = RequestMethod.POST)
    public Map<String, Object> getAPK(String page, String size, String keyword, String shopUserId, String token,
            String loginRoleId, String menuId, String assetsType, String doorType, Map<String, Object> map)
    {
        try
        {
            apkService.getAPK(page, size, keyword, shopUserId, token, loginRoleId, menuId, assetsType, doorType, map);
        }
        catch (NumberFormatException e)
        {
            logger.error("查询APK失败：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("查询APK失败：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        return successData(map);
    }

    /**
     * 获取推送app安装包的终端
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param assetsType 资产类型（0：柜子，1：门店）
     * @param doorType 门类型（1：单门，2：双门）
     * @param page 页数
     * @param size 大小
     * @param keyword 关键字（终端id；序列号）
     * @param version 版本号
     * @return
     */
    @RequestMapping(value = "/api/getPushAPKTerminal", method = RequestMethod.POST)
    public Map<String, Object> getPushAPKTerminal(String shopUserId, String token, String assetsType, String doorType,
            String page, String size, String keyword, String version)
    {
        Map<String, Object> map=new HashMap<String, Object>();
        try
        {
            map=apkService.getPushAPKTerminal(shopUserId, token, assetsType, doorType, page, size, keyword,
                    version);
        }
        catch (NumberFormatException e)
        {
            logger.error("获取推送app安装包的终端失败：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("获取推送app安装包的终端失败：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("获取推送app安装包的终端失败：", e);
            e.printStackTrace();
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 推送APK
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param apkEntity apk json串
     * @param assetsType 资产类型（0：柜子，1：门店）
     * @param doorType 门类型（1：单门，2：双门）
     * @param terminalIdList 终端集合
     * @return
     */
    @RequestMapping(value = "/api/pushAPK", method = RequestMethod.POST)
    public Map<String, Object> pushAPK(String shopUserId, String token, String apkEntity, String assetsType,
            String doorType, String terminalIdList)

    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            apkService.pushAPK(shopUserId, token, apkEntity, assetsType, doorType, terminalIdList);
        }
        catch (NumberFormatException e)
        {
            logger.error("推送APK失败：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("推送APK失败：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("推送APK失败：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }

    /**
     * 获取推送安装包日志
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param keyword 关键字（终端id；版本号；序列号）
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param page 页数
     * @param size 大小
     * @param type 类型
     * @param doorType 门类型（1：单门，2：双门）
     * @param map
     * @return
     */
    @RequestMapping(value = "/api/getPushAPKLogByKeyword", method = RequestMethod.POST)
    public Map<String, Object> getPushAPKLogByKeyword(String shopUserId, String token, String keyword, String startTime,
            String endTime, String page, String size, String type, String doorType, Map<String, Object> map)
    {
        try
        {
            apkService.getPushAPKLogByKeyword(shopUserId, token, keyword, startTime, endTime, page, size, type,
                    doorType, map);
        }
        catch (NumberFormatException e)
        {
            logger.error("获取推送安装包日志：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("获取推送安装包日志：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        return successData(map);
    }

    /**
     * 删除apk
     * @param shopUserId 商家用户id
     * @param token 验签
     * @param id 主键id
     * @return
     */
    @RequestMapping(value = "/api/deleteAPK", method = RequestMethod.POST)
    public Map<String, Object> deleteAPK(String shopUserId, String token,String id)

    {
        Map<String, Object> map = new HashMap<String, Object>();
        try
        {
            apkService.deleteAPK(shopUserId, token, id);
        }
        catch (NumberFormatException e)
        {
            logger.error("删除apk：", e);
            return paramFailedMessage();
        }
        catch (InnerException e)
        {
            logger.error("删除apk：", e);
            return failedData(map, e.getErrorCode(), e.getMessage());
        }
        catch (Exception e)
        {
            logger.error("删除apk：", e);
            return failedData(map, ErrorCodeConstant.FAILED, ErrorCodeConstant.FAILED_MSG);
        }
        return successData(map);
    }
}
